from fastmcp import FastMCP
from typing import Dict, Any
import math

class CalculatorServer:
    def __init__(self):
        self.server = FastMCP("Calculator Server")
        self.setup_tools()

    def setup_tools(self):
        # Register tools with their names
        self.server.tool("add")(self.add)
        self.server.tool("subtract")(self.subtract)
        self.server.tool("multiply")(self.multiply)
        self.server.tool("divide")(self.divide)
        self.server.tool("square_root")(self.square_root)
        self.server.tool("square")(self.square)
        self.server.tool("sin")(self.sin)
        self.server.tool("cos")(self.cos)
        self.server.tool("analyze_number")(self.analyze_number)

    async def add(self, params: Dict[str, Any]) -> Dict[str, Any]:
        a, b = params["a"], params["b"]
        return {"result": a + b}

    async def subtract(self, params: Dict[str, Any]) -> Dict[str, Any]:
        a, b = params["a"], params["b"]
        return {"result": a - b}

    async def multiply(self, params: Dict[str, Any]) -> Dict[str, Any]:
        a, b = params["a"], params["b"]
        return {"result": a * b}

    async def divide(self, params: Dict[str, Any]) -> Dict[str, Any]:
        a, b = params["a"], params["b"]
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return {"result": a / b}

    async def square_root(self, params: Dict[str, Any]) -> Dict[str, Any]:
        number = params["number"]
        if number < 0:
            raise ValueError("Cannot calculate square root of negative number")
        return {"result": math.sqrt(number)}

    async def square(self, params: Dict[str, Any]) -> Dict[str, Any]:
        number = params["number"]
        return {"result": number ** 2}

    async def sin(self, params: Dict[str, Any]) -> Dict[str, Any]:
        angle = params["angle"]
        rad = math.radians(angle)
        return {"result": math.sin(rad)}

    async def cos(self, params: Dict[str, Any]) -> Dict[str, Any]:
        angle = params["angle"]
        rad = math.radians(angle)
        return {"result": math.cos(rad)}

    async def analyze_number(self, params: Dict[str, Any]) -> Dict[str, Any]:
        number = params["number"]
        return {
            "is_positive": number > 0,
            "is_negative": number < 0,
            "is_zero": number == 0,
            "absolute_value": abs(number),
            "integer_part": int(number),
            "decimal_part": number - int(number)
        }

    def run(self):
        self.server.run()

if __name__ == "__main__":
    calculator = CalculatorServer()
    calculator.run()